<?php
declare (strict_types=1);

namespace app\api\controller\v3;

use app\common\model\Course;
use app\common\model\Grade;
use app\common\model\Unit as UnitModel;
use app\common\model\Error;

class ErrorLog extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return sucessMsg(200, '获取成功', [
            'book' => $this->book(),   //课本名称
//            'list' => $this->getList(),     //  获取错题信息
        ]);
    }

    //课本名称
    public function book()
    {
        //左侧课本名称
        $model = new Error();
        $data = $model->order('id', 'desc')->field('id,name,grade_id,unit_id')->select();
        foreach ($data as &$v) {
            $v['grade_id'] = $v->grade->title;
            $v['unit_id'] = $v->unit->title;
//            unset($v->grade);
//            unset($v->unit);
        }
        //右侧详情
        $grade_id = input('grade_id/d', 1);
        $unit_id = input('unit_id/d', 1);
        $type = input('type/d', 1);
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 15);
        $where['grade_id'] = $grade_id;
        $where['unit_id'] = $unit_id;
        $where['type'] = $type;
        $arr = $model->where($where)->page($page, $pageSize)->select();
        return ['data' => $data, 'arr' => $arr];
    }

    /*
     * 获取错误信息
     * */
//    public function getLists()
//    {
//        $id = input('id/d',1);
//        $type = input('type/d', 1);
//        $page = input('page/d', 1);
//        $pageSize = input('pageSize/d', 15);
//        $model = new Error();
//        $list = $model->where(['type' => $type, 'id' => $id])->order(['id' => 'desc'])->page($page, $pageSize)->select()->toArray();
//        foreach ($list as $k => $v) {
//            $grade = Grade::find($v['grade_id']);   //  年级id
//            $unit = UnitModel::find($v['unit_id']);     //  单元id
//            $course = Course::find($v['course_id']);    //  分类
//            $list[$k]['grade_name'] = $grade->title;
//            $list[$k]['unit_name'] = $unit->title;
//            $list[$k]['course_name'] = $course->title;
//            $list[$k]['image'] = config("app.domain_url") . $list[$k]['image'];
//            $list[$k]['video'] = config("app.domain_url") . $list[$k]['video'];
//        }
//        return $list;
//    }
}
